%%%-------------------------------------------------------------------
%%% File    : iter_unzip.erl
%%% Author  :  <vjache>
%%% Description : 
%%%
%%% Created : 22 Jul 2009 by  <vjache>
%%%-------------------------------------------------------------------
-module(iter_unzip).

-include("../include/iter.hrl").
-include("../include/zip.hrl").

%% API
-export([new/1,new/2]).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: 
%% Description:
%%--------------------------------------------------------------------
new(ensure_order,BaseIter) when is_record(BaseIter, iter)->
	iter_concat:new(
	  iter_mapper:new(
		BaseIter, 
		fun(?ZIP_LIST_NOTREVERSED(_Length,ZipBinary))->
				iter:from_list(lists:reverse(binary_to_term(zlib:unzip(ZipBinary))));
		   (?ZIP_LIST_REVERSED(_Length,ZipBinary))->
				iter:from_list(binary_to_term(zlib:unzip(ZipBinary)));
		   (NonZippedItem) ->
				NonZippedItem
		end)).
new(BaseIter) when is_record(BaseIter, iter)->
	iter_concat:new(
	  iter_mapper:new(
		BaseIter, 
		fun({Tag,_Length,ZipBinary}) when 
			 Tag==?ZIP_LIST_NOTREVERSED_TAG orelse Tag==?ZIP_LIST_REVERSED_TAG ->
				iter:from_list(binary_to_term(zlib:unzip(ZipBinary)));
		   (NonZippedItem) ->
				NonZippedItem
		end)).
%%====================================================================
%% Internal functions
%%====================================================================

